You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.3 KiB
43 lines
1.3 KiB
import { updatePost } from "#server/service/posts";
|
|
import { visibilitySchema } from "#server/constants/visibility";
|
|
import { isUniqueConstraintViolation } from "#server/utils/db-unique-constraint";
|
|
|
|
export default defineWrappedResponseHandler(async (event) => {
|
|
const user = await event.context.auth.requireUser();
|
|
const id = Number(event.context.params?.id);
|
|
if (!Number.isInteger(id)) {
|
|
throw createError({ statusCode: 400, statusMessage: "无效 id" });
|
|
}
|
|
const body = await readBody<{
|
|
title?: string;
|
|
slug?: string;
|
|
bodyMarkdown?: string;
|
|
excerpt?: string;
|
|
coverUrl?: string | null;
|
|
tagsJson?: string;
|
|
publishedAt?: string | null;
|
|
visibility?: string;
|
|
}>(event);
|
|
|
|
try {
|
|
const post = await updatePost(user.id, id, {
|
|
...body,
|
|
publishedAt:
|
|
body.publishedAt !== undefined
|
|
? body.publishedAt
|
|
? new Date(body.publishedAt)
|
|
: null
|
|
: undefined,
|
|
visibility: body.visibility !== undefined ? visibilitySchema.parse(body.visibility) : undefined,
|
|
});
|
|
if (!post) {
|
|
throw createError({ statusCode: 404, statusMessage: "未找到" });
|
|
}
|
|
return R.success({ post });
|
|
} catch (e) {
|
|
if (isUniqueConstraintViolation(e)) {
|
|
throw createError({ statusCode: 409, statusMessage: "slug 已存在" });
|
|
}
|
|
throw e;
|
|
}
|
|
});
|
|
|